package com.mjf.kxml.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.common.utils.DateUtils;
import com.mjf.kxml.domain.ContractInfo;
import com.mjf.kxml.enums.CKApiEnum;
import com.mjf.kxml.enums.SendFlagEnum;
import com.mjf.kxml.mapper.ContractInfoMapper;
import com.mjf.kxml.service.ContractInfoService;
import com.mjf.kxml.util.CKHttpSendUtil;
import com.mjf.kxml.util.CKResultVO;
import com.mjf.kxml.vo.ContractLogQuery;
import com.mjf.kxml.vo.ContractLogResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/4 15:41
 */
@Slf4j
@Service
public class ContractInfoServiceImpl extends ServiceImpl<ContractInfoMapper, ContractInfo> implements ContractInfoService {
    @Override
    public CKResultVO sendAll() {
        log.info("开始推送合同数据");
        // 查询需要推送的数据
        List<ContractInfo> infoss = list(Wrappers.<ContractInfo>lambdaQuery().ne(ContractInfo::getPullsk,
            SendFlagEnum.SEND_FLAG_YES.getCodes()));
        if (CollectionUtil.isNotEmpty(infoss)) {
            for (ContractInfo fo : infoss) {
                List<ContractInfo> infos = Collections.singletonList(fo);
                try {
                    String soapXml = ContractInfo.buildObjectXml(IdUtil.fastSimpleUUID(), infos);
                    CKResultVO ckResultVO = CKHttpSendUtil.sendXml(CKApiEnum.CK_API_CONTRACT, soapXml);
                    fo.setPullskoutstr(ckResultVO.getData());
                    fo.setPulltime(LocalDateTime.now());
                    if (ckResultVO.getSuccess()) {
                        fo.setPullsk(SendFlagEnum.SEND_FLAG_YES.getCodes());
                    } else {
                        fo.setPullsk(SendFlagEnum.SEND_FLAG_FAIL.getCodes());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("发送合同数据异常 ", e);
                    fo.setPullsk(SendFlagEnum.SEND_FLAG_FAIL.getCodes());
                    fo.setPullskoutstr(e.getMessage());
                    fo.setPulltime(LocalDateTime.now());
                }
            }
            updateBatchById(infoss);
        }
        log.info("合同推送完成");
        return CKResultVO.buildSuccess("S", "推送完成", "");
    }

    @Override
    public IPage<ContractLogResVO> contractLogs(ContractLogQuery query) {
        IPage<ContractLogResVO> res = new Page<>();
        res.setRecords(new ArrayList<>());
        res.setPages(query.getPage());
        res.setSize(query.getSize());
        LambdaQueryWrapper<ContractInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(StrUtil.isNotBlank(query.getPullsk()), ContractInfo::getPullsk, query.getPullsk());
        qw.ge(StrUtil.isNotBlank(query.getStart()), ContractInfo::getPulltime, query.getStart());
        qw.le(StrUtil.isNotBlank(query.getEnd()), ContractInfo::getPulltime, query.getEnd());
        int count = count(qw);
        res.setTotal(count);
        if (count < 1) {
            return res;
        }
        qw.last(" limit " + (query.getPage() - 1) * query.getSize() + "," + query.getSize());
        List<ContractInfo> list = list(qw);
        res.setRecords(list.stream().map(e -> ContractLogResVO.builder()
            .id(e.getId())
            .ccarGsname(e.getCcarGsname())
            .ccarGscode(e.getCcarGscode())
            .number(e.getNumber())
            .name(e.getName())
            .ccarCreateUser(e.getCcarCreateUser())
            .ccarCreateUserCode(e.getCcarCreateUserCode())
            .ccarIsDeleted(e.getCcarIsDeleted())
            .ccarAcntmcode(e.getCcarAcntmcode())
            .ccarAcntmname(e.getCcarAcntmname())
            .ccarOppositeCompany(e.getCcarOppositeCompany())
            .ccarOppositeCompanycode(e.getCcarOppositeCompanycode())
            .ccarContractType(e.getCcarContractType())
            .ccarContractSubject(e.getCcarContractSubject())
            .ccarBillingType(e.getCcarBillingType())
            .ccarSignDate(e.getCcarSignDate())
            .ccarStartDate(e.getCcarStartDate())
            .ccarEndDate(e.getCcarEndDate())
            .ccarFrameworkContract(e.getCcarFrameworkContract())
            .ccarContractAmount(e.getCcarContractAmount())
            .ccarContractAmtwotax(e.getCcarContractAmtwotax())
            .ccarSettlementAmount(e.getCcarSettlementAmount())
            .ccarCurrenctcode(e.getCcarCurrenctcode())
            .ccarSettlementTerms(e.getCcarSettlementTerms())
            .ccarRetention(e.getCcarRetention())
            .ccarRetentionRatio(e.getCcarRetentionRatio())
            .ccarRetentionTerms(e.getCcarRetentionTerms())
            .ccarAmendment(e.getCcarAmendment())
            .ccarSupplement(e.getCcarSupplement())
            .ccarBaseContract(e.getCcarBaseContract())
            .ccarStandby1(e.getCcarStandby1())
            .ccarStandby2(e.getCcarStandby2())
            .ccarStandby3(e.getCcarStandby3())
            .ccarStandby4(e.getCcarStandby4())
            .ccarStandby5(e.getCcarStandby5())
            .ccarStandby6(e.getCcarStandby6())
            .ccarStandby7(e.getCcarStandby7())
            .ccarStandby8(e.getCcarStandby8())
            .ccarStandby9(e.getCcarStandby9())
            .ccarStandby10(e.getCcarStandby10())
            .times(DateUtils.toLocalDateTimeString(e.getTimes()))
            .pullsk(e.getPullsk())
            .pullskoutstr(e.getPullskoutstr())
            .pulltime(DateUtils.toLocalDateTimeString(e.getPulltime()))
            .build()).collect(Collectors.toList()));
        return res;
    }
}
